<?php
namespace Admin\Controller;

class NewsController extends AdminController {
    /**
     * 资讯列表
     */
    public function index() {
        $title = M('news_type')->select();
        $type = I('get.type');
        $smallType = M('news_type')->limit(1)->select();//默认分类
        $type = empty($type)?$smallType[0]['id']:$type;
        $typeName = M('news_type')->where('id='.$type)->find();
        $list = $this->lists('news', array('type_id'=>$type));

        $this->assign('_list', $list);
        $this->assign('title', $title);
        $this->assign('titleName', $typeName['title']);
        $this->meta_title = "资讯列表";
        $this->display();
    }
    /**
     * 增加资讯
     */
    public function add() {
        $title = M('news_type')->select();
        if (IS_POST) {
            $data = array(
                'title'=>I('post.title'),
                'is_hot'=>I('post.is_hot'),
                'create_time'=>NOW_TIME,
                'update_time' => NOW_TIME,
                'type_id'=>I('post.type'),
                'content'=>I('post.content'),
                'movie_id'=>I('post.movie_id')
            );
            empty($data['title']) && $this->error('标题不能为空');
            (mb_strlen($data['title']) < 2 || mb_strlen($data['title']) > 100) && $this->error('标题长度不能小于4或大于100字符');
            $id = M('news')->add($data);
            $id > 0 ? $this->success('操作成功', U('index',array('type'=>I('post.type')))) : $this->error('操作失败，未知错误');
        }
        $this->assign('title',$title);
        $this->display();
    }
    /**
     * 编辑资讯
     */
    public function edit() {
        $id = I('get.id');
        $title = M('news_type')->select();
        if (IS_POST) {
            $data = array(
                'title'=>I('post.title'),
                'is_hot'=>I('post.is_hot'),
                'update_time' => NOW_TIME,
                'type_id'=>I('post.type'),
                'content'=>I('post.content'),
                'movie_id'=>I('post.movie_id')
            );
            empty($data['title']) && $this->error('标题不能为空');
            (mb_strlen($data['title']) < 4 || mb_strlen($data['title']) > 100) && $this->error('标题长度不能小于4或大于100字符');
            $id = M('news')->where('id='.I('post.id'))->save($data);
            $id > 0 ? $this->success('操作成功', U('index')) : $this->error('操作失败，未知错误');
        } else {
            $newsInfo = M('news')->where('id='.$id)->find();
            $this->assign('newsInfo', $newsInfo);
            $this->assign('title',$title);
            $this->display('edit');
        }
    }
    /**
     * 删除资讯
     */
    public function delete() {
        $id = I('get.id', 0);
        $info = M('news')->find($id);
        $info || $this->error('参数有误');
        $status = M('news')->where(array('id'=>$id))->delete();
        $status !== false ? $this->success('操作成功') : $this->error('未知错误');
    }
    /**
     * 资讯栏目列表
     */
    public function typeIndex() {
        $list = $this->lists('news_type', array(), 'null');
        $this->assign('_list', $list);
        $this->display('typeIndex');
    }
    /**
     * 增加资讯栏目
     */
    public function typeAdd() {
        $data['title'] = I('post.typeName');
        if (IS_POST) {
            $newsType = M('news_type');
            $typeResult = $newsType->add($data);
            $typeResult > 0 ? $this->success('操作成功', U('typeIndex')) : $this->error('操作失败，未知错误');
        } else {
            $this->display('typeAdd');
        }
    }
    /**
     * 修改资讯栏目
     */
    public function typeEdit() {
        $id = I('post.id');
        $data = array(
            'id' => $id,
            'title' => I('post.title')
        );
        $model = M('news_type');
        if (IS_POST) {
            $result = $model->where('id='.$id)->save($data);
            $result !== false ? $this->success('操作成功', U('typeIndex')) : $this->error('操作失败，未知错误');
        } else {
            $titleInfo = $model->where('id='.I('get.id'))->find();
            $this->assign('titleInfo', $titleInfo);
            $this->display('typeEdit');
        }
    }
    /**
     * 删除资讯栏目
     */
    public function typeDelete() {
        $id = I('get.id');
        $model = M('news_type');
        $info = $model->where('id='.$id)->find();
        if (!$info) {
            $this->error('参数不正确');
        }
        $result = $model->where('id='.$id)->delete();
        $result !== false ? $this->success('操作成功') : $this->error('未知错误');
    }
}